/*
 * spi.c
 *
 *  Created on: 09.04.2014
 *      Author: alexs
 */

#include "spi.h"
#define SC_select() PORTC&=~(1<<PC1)
#define SC_unselect() PORTC|=(1<<PC1)


void spi_init(void) __attribute__ ((naked)) __attribute__ ((section (".init7")));
void spi_init() {
	/* SPI */
	PORTC|=(1<<PC1);
	DDRC|=(1<<PC1);

	PORTB|=(1<<PB2);
	DDRB|=(1<<PB3)|(1<<PB5)|(1<<PB2);

	DDRD|=(1<<PD7);
	PORTD|=(1<<PD7);
	SPCR=(1<<SPE)|(0<<DORD)|(1<<MSTR)|(0<<CPOL)|(0<<CPHA)|(0<<SPR1)|(0<<SPR0);
	SPSR=0;
}

void spi_transfern(char * buff, uint16_t len){
	uint16_t i;
//	uint8_t tmp;

	SC_select();
	for (i=0;i<len;i++) {
		SPDR=*buff;
		while (!(SPSR&(1<<SPIF)));
		*(buff++)=SPDR;
	}
	SC_unselect();
}
